/*
* For getting FormIds to push template information for users.
* 说明：
* 1.请在需要保存FormId的组件外层，使用form组件包裹，并设置report-submit属性为formConfig.reportSubmit[即：true]，并设置bindsubmit回调函数为[handleName]；
* 2.请务必传page，值为当前使用组件的页面this；
* 3.success,fail,complete分别对应保存FormId成功、失败、完成的回调函数。
*/
var requester = require("../common/requester");
var service = require("../common/config").service;

function getFormId(config = {
    page: null,
    handleName: "submitHandle"
}, success = null, fail = null, complete = null) {
    let {page, handleName, callFrom} = config;
    if (!page) {
        console.error("The param 'config.page' is required");
        return;
    }
    handleName = handleName || "submitHandle";

    let formConfig = {reportSubmit: true};
    page.setData({formConfig});

    page[handleName] = (evt) => {
        if (evt && evt.detail) {
            const {formId} = evt.detail;
            const callFrom = evt.detail.target.dataset.idx || 1;
            requester.request({
                url: service.saveFormId,
                data: {
                    formId,
                    callFrom
                }
            });
        } else {
            console.err("event error: There is no property detail");
        }
    };
}

module.exports = getFormId;